<?php

/*
php.exe -f "c:\Projects\Stachovsti\montanus-drupal\www\test-calendar-occupation.php"
*/

/*
$bookingType: 0 - free, 1 - reservation (waiting for full or advanced payment), 2 - fully booked (full or advanced payment)
return array(
'2013-09-01' => array('pr' => 2, 'kn' => 0, 'ch' => 0),
'2013-10-01' => array('pr' => 1, 'kn' => 2, 'ch' => 0));
*/
function mt_booking_create_calendar_occupation_set($period, $accomodation, $bookingType, &$result) {
	foreach ($period as $day) {
		$key = $day->format('Y-m-d');
		if (array_key_exists($key, $result)) {
			// some accomodation is already booking by other submission at this $day
			if (!$result[$key]['pr']) { $result[$key]['pr'] = in_array('pr', $accomodation) ? $bookingType : 0; }
			if (!$result[$key]['kn']) { $result[$key]['kn'] = in_array('kn', $accomodation) ? $bookingType : 0; }
			if (!$result[$key]['ch']) { $result[$key]['ch'] = in_array('ch', $accomodation) ? $bookingType : 0; }
		}
		else {
			// nothing is booked at this $day
			$result[$key] = array(
				'pr' => in_array('pr', $accomodation) ? $bookingType : 0,
				'kn' => in_array('kn', $accomodation) ? $bookingType : 0,
				'ch' => in_array('ch', $accomodation) ? $bookingType : 0
			);
		}
	}
}

function mt_booking_create_calendar_occupation($submissons, $today) {
	
	//$submissons = webform_get_submissions(5);
	//$today = new DateTime();
	
	$result = array();
	
	foreach($submissons as $s) {
		$departure = DateTime::createFromFormat('Y-m-d', $s->data[2]['value'][0]);
		// only future submission is important
		if ($departure <= $today) {
			continue;
		}
		
		// full payment or advanced payment done => booked period is going to be booked
		if (isset($s->data[15]['value'][0])
		|| (array_key_exists(12, $s->data) && intval($s->data[12]['value'][0]) > 0)) {
			$accomodation = $s->data[3]['value'];
			$arrival = DateTime::createFromFormat('Y-m-d', $s->data[1]['value'][0]);
			$start = $arrival < $today ? $today : $arrival;
			$interval = DateInterval::createFromDateString('1 day');
			$period = new DatePeriod($start, $interval, $departure);
			mt_booking_create_calendar_occupation_set($period, $accomodation, 2, $result);
			continue;
		}
		
		$submittedStamp = $s->submitted;
		$end = new DateTime("@$submittedStamp");
		$end->add(new DateInterval('P7D'));
		// protection period for advanced payment is over
		if ($end <= $today) {
			continue;
		}
		
		// advanced payment protection period
		$arrival = DateTime::createFromFormat('Y-m-d', $s->data[1]['value'][0]);
		$start = $arrival < $today ? $today : $arrival;
		$interval = DateInterval::createFromDateString('1 day');
		$period = new DatePeriod($start, $interval, $departure);
		$accomodation = $s->data[3]['value'];
		mt_booking_create_calendar_occupation_set($period, $accomodation, 1, $result);
	}
	
	return $result;
}

function mt_booking_is_date_occupied($arrival, $departure, $accomodation, $submissons, $today) {
	$calendar = mt_booking_create_calendar_occupation($submissons, $today);
	$interval = DateInterval::createFromDateString('1 day');
	$period = new DatePeriod($arrival, $interval, $departure);
	foreach ($period as $day) {
		$key = $day->format('Y-m-d');
		if (array_key_exists($key, $calendar)) {
			foreach ($accomodation as $acc) {
				if ($calendar[$key][$acc]) {
					return $calendar;
				}
			}
		}
	}
	return false;
}


$submission = array();
$submission[0] = new stdClass();
$today = DateTime::createFromFormat('Y-m-d', '2013-10-10');

function test_reset() {
	global $submission, $today;
	$submission = array();
	$submission[0] = new stdClass();
	$today = DateTime::createFromFormat('Y-m-d', '2013-10-10');
}


// ---------- full price is paied without advanced payment
test_reset();
$today = DateTime::createFromFormat('Y-m-d', '2013-10-18');
$submission[0]->submitted = 1381280416; //'2013-10-09'
$submission[0]->data = array(
	1 => array( 'value' => array( 0 => '2013-11-11')),
	2 => array( 'value' => array( 0 => '2013-11-18')),
	3 => array( 'value' => array( 0 => 'ch' )),
	15=> array( 'value' => array( 0 => true )));
$result = mt_booking_is_date_occupied(DateTime::createFromFormat('Y-m-d', '2013-11-11'), DateTime::createFromFormat('Y-m-d', '2013-11-18'), array('ch'), $submission, $today);

echo("full price is paied without advanced payment:\n" . ($result ? "success" : "!!!FAILED!!!") . "\n");


// ---------- the same reservation for the second time
test_reset();
$submission[0]->submitted = 1381280416; //'2013-10-09'
$submission[0]->data = array(
	1 => array( 'value' => array( 0 => '2013-11-11')),
	2 => array( 'value' => array( 0 => '2013-11-18')),
	3 => array( 'value' => array( 0 => 'ch' )));
$result = mt_booking_is_date_occupied(DateTime::createFromFormat('Y-m-d', '2013-11-11'), DateTime::createFromFormat('Y-m-d', '2013-11-18'), array('ch'), $submission, $today);

echo("the same reservation for the second time:\n" . ($result ? "success" : "!!!FAILED!!!") . "\n");


// ---------- mt_booking_is_date_occupied
test_reset();
$submission[0]->submitted = 1381280416; //'2013-10-09'
$submission[0]->data = array(
	1 => array( 'value' => array( 0 => '2013-10-11')),
	2 => array( 'value' => array( 0 => '2013-10-14')),
	3 => array( 'value' => array( 0 => 'pr' )),
	12 => array( 'value' => array( 0 => 1234 )));
$result = mt_booking_is_date_occupied(DateTime::createFromFormat('Y-m-d', '2013-10-10'), DateTime::createFromFormat('Y-m-d', '2013-10-12'), array('pr'), $submission, $today);

echo("mt_booking_is_date_occupied:\n" . ($result ? "success" : "!!!FAILED!!!") . "\n");


// ---------- only future is important
test_reset();
$submission[0]->data = array( 2 => array( 'value' => array( 0 => '2013-10-10')));
$result = mt_booking_create_calendar_occupation($submission, $today);

echo("only future is important:\n" . (empty($result) ? "success" : "!!!FAILED!!!") . "\n");


// ---------- advanced payment done, today is in the middle of the period
test_reset();
$submission[0]->data = array(
	1 => array( 'value' => array( 0 => '2013-10-09')),
	2 => array( 'value' => array( 0 => '2013-10-12')),
	3 => array( 'value' => array( 0 => 'kn', 1 => 'ch')),
	12 => array( 'value' => array( 0 => 1234 )));
$result = mt_booking_create_calendar_occupation($submission, $today);

$condition = count($result) == 2 && array_key_exists('2013-10-10', $result) && array_key_exists('2013-10-11', $result)
	&& !$result['2013-10-10']['pr'] && $result['2013-10-10']['kn'] && $result['2013-10-10']['ch']
	&& !$result['2013-10-11']['pr'] && $result['2013-10-11']['kn'] && $result['2013-10-11']['ch'];
echo("advanced payment done, today is in the middle of the period:\n" . ($condition ? "success" : "!!!FAILED!!!") . "\n");


// ---------- advanced payment done, whole period is in the future
test_reset();
$submission[0]->data = array(
	1 => array( 'value' => array( 0 => '2013-10-10')),
	2 => array( 'value' => array( 0 => '2013-10-12')),
	3 => array( 'value' => array( 0 => 'kn', 1 => 'ch')),
	12 => array( 'value' => array( 0 => 1234 )));
$result = mt_booking_create_calendar_occupation($submission, $today);

$condition = count($result) == 2 && array_key_exists('2013-10-10', $result) && array_key_exists('2013-10-11', $result)
	&& !$result['2013-10-10']['pr'] && $result['2013-10-10']['kn'] && $result['2013-10-10']['ch']
	&& !$result['2013-10-11']['pr'] && $result['2013-10-11']['kn'] && $result['2013-10-11']['ch'];
echo("advanced payment done, whole period is in the future:\n" . ($condition ? "success" : "!!!FAILED!!!") . "\n");


// ---------- advanced payment done, some accomodation is already booking by other submission at this day
test_reset();
$submission[0]->data = array(
	1 => array( 'value' => array( 0 => '2013-10-10')),
	2 => array( 'value' => array( 0 => '2013-10-12')),
	3 => array( 'value' => array( 0 => 'pr', 1 => 'ch')),
	12 => array( 'value' => array( 0 => 1234 )));
$submission[1] = new stdClass();
$submission[1]->data = array(
	1 => array( 'value' => array( 0 => '2013-10-10')),
	2 => array( 'value' => array( 0 => '2013-10-12')),
	3 => array( 'value' => array( 0 => 'kn', 1 => 'ch')),
	12 => array( 'value' => array( 0 => 1234 )));
$result = mt_booking_create_calendar_occupation($submission, $today);

$condition = count($result) == 2 && array_key_exists('2013-10-10', $result) && array_key_exists('2013-10-11', $result)
	&& $result['2013-10-10']['pr'] && $result['2013-10-10']['kn'] && $result['2013-10-10']['ch']
	&& $result['2013-10-11']['pr'] && $result['2013-10-11']['kn'] && $result['2013-10-11']['ch'];
echo("advanced payment done, whole period is in the future:\n" . ($condition ? "success" : "!!!FAILED!!!") . "\n");


// ---------- protection period for advanced payment is over
test_reset();
$today = DateTime::createFromFormat('Y-m-d', '2013-10-18');
$submission[0]->submitted = 1381280416; //'2013-10-09'
$submission[0]->data = array( 2 => array( 'value' => array( 0 => '2013-10-29')), 12 => array( 'value' => array( 0 => null )));
$result = mt_booking_create_calendar_occupation($submission, $today);

echo("protection period for advanced payment is over:\n" . (empty($result) ? "success" : "!!!FAILED!!!") . "\n");


// ---------- inside advanced payment protection period
test_reset();
$today = DateTime::createFromFormat('Y-m-d', '2013-10-14');
$submission[0]->submitted = 1381280416; //'2013-10-09'
$submission[0]->data = array(
	1 => array( 'value' => array( 0 => '2013-10-20')),
	2 => array( 'value' => array( 0 => '2013-10-25')),
	3 => array( 'value' => array( 0 => 'pr', 1 => 'ch')),
	12 => array( 'value' => array( 0 => null )));
$result = mt_booking_create_calendar_occupation($submission, $today);

echo("inside advanced payment protection period:\n" . (count($result) == 5 ? "success" : "!!!FAILED!!!") . "\n");


//var_dump($result);

?>
